<?php

class DB_Sql {
  var $Host     = "localhost";
  var $Database = "opd";
  var $User     = "root";
  var $Password = "root";
  var $Auto_Free     = 0;
  var $Debug         = 0;
  var $Halt_On_Error = "yes";
  var $Seq_Table     = "db_sequence";
  var $Record   = array();
  var $Row;
  var $Errno    = 0;
  var $Error    = "";
  var $type     = "mysql";
  var $revision = "1.2";
  var $Link_ID  = 0;
  var $Query_ID = 0;
  var $AES_key = 'vxl6itkjzv';


  function DB_Sql($query = "") {
      $this->query($query);
  }

  function link_id() {
    return $this->Link_ID;
  }

  function query_id() {
    return $this->Query_ID;
  }

  function connect($Database = "", $Host = "", $User = "", $Password = "") {
    if ("" == $Database)
      $Database = $this->Database;
    if ("" == $Host)
      $Host     = $this->Host;
    if ("" == $User)
      $User     = $this->User;
    if ("" == $Password)
      $Password = $this->Password;

    if ( 0 == $this->Link_ID ) {
      $this->Link_ID=mysql_pconnect($Host, $User, $Password);
      if (!$this->Link_ID) {
        $this->halt("pconnect($Host, $User, \$Password) failed.");
        return 0;
      }

      if (!@mysql_select_db($Database,$this->Link_ID)) {
        $this->halt("cannot use database ".$this->Database);
        return 0;
      }
    }

    return $this->Link_ID;
  }

  function free() {
      @mysql_free_result($this->Query_ID);
      $this->Query_ID = 0;
  }

  function query($Query_String) {
    if ($Query_String == "")
      return 0;

    if (!$this->connect()) {
      return 0;
    };

    if ($this->Query_ID) {
      $this->free();
    }

    if ($this->Debug){
	//printf("Debug: query = %s<br>\n", $Query_String);
	$time_start = microtime_float();
    }


    $this->Query_ID = @mysql_query($Query_String,$this->Link_ID);
    $this->Row   = 0;
    $this->Errno = mysql_errno();
    $this->Error = mysql_error();
    if (!$this->Query_ID) {
      $this->halt("Invalid SQL: ".$Query_String);
    }

    return $this->Query_ID;
  }

  function next_record() {
    if (!$this->Query_ID) {
      $this->halt("next_record called with no query pending.");
      return 0;
    }

    $this->Record = @mysql_fetch_assoc($this->Query_ID);
    $this->Row   += 1;
    $this->Errno  = mysql_errno();
    $this->Error  = mysql_error();

    $stat = is_array($this->Record);
    if (!$stat && $this->Auto_Free) {
      $this->free();
    }
    return $stat;
  }

  function seek($pos = 0) {
    $status = @mysql_data_seek($this->Query_ID, $pos);
    if ($status)
      $this->Row = $pos;
    else {
      $this->halt("seek($pos) failed: result has ".$this->num_rows()." rows");
      @mysql_data_seek($this->Query_ID, $this->num_rows());
      $this->Row = $this->num_rows;
      return 0;
    }

    return 1;
  }

  
  function affected_rows() {
    return @mysql_affected_rows($this->Link_ID);
  }

  function num_rows() {
    return @mysql_num_rows($this->Query_ID);
  }

  function num_fields() {
    return @mysql_num_fields($this->Query_ID);
  }

  function last_id(){
    return @mysql_insert_id($this->Link_ID);
  }

  function total() {
    return $this->num_rows();
  }

  function nf() {
    return $this->num_rows();
  }

  function np() {
    print $this->num_rows();
  }

  function f($Name) {
    return $this->Record[$Name];
  }

  function p($Name) {
    print $this->Record[$Name];
  }

  function halt($msg) {
    $this->Error = @mysql_error($this->Link_ID);
    $this->Errno = @mysql_errno($this->Link_ID);
    if ($this->Halt_On_Error == "no")
      return;

    $this->haltmsg($msg);

    if ($this->Halt_On_Error != "report")
      die("Session halted.");
  }

  function haltmsg($msg) {
    printf("</td></tr></table><b>Database error:</b> %s<br>\n", $msg);
    printf("<b>MySQL Error</b>: %s (%s)<br>\n",
      $this->Errno,
      $this->Error);
  }

  function getArray(){
    $arr = array();
    while($this->next_record()){
      $arr[] = $this->Record;
    }
    return $arr;
  }
}

?>
